Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hookable

Package Overview
Dependencies
Maintainers
2
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hookable

Awaitable hook system

  • 5.5.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.3M
decreased by-1.51%
Maintainers
2
Weekly downloads
 
Created

What is hookable?

The 'hookable' npm package provides a way to create and manage hooks in JavaScript applications. Hooks are functions that can be registered and executed at certain points in your application, allowing for extensibility and customization.

What are hookable's main functionalities?

Creating and Registering Hooks

This feature allows you to create and register hooks. In the example, a hook named 'myHook' is created and a function is registered to it. When 'myHook' is called, the registered function is executed.

const { Hookable } = require('hookable');
const hooks = new Hookable();

hooks.hook('myHook', async () => {
  console.log('Hook executed!');
});

hooks.callHook('myHook');

Adding Multiple Handlers to a Hook

This feature allows you to add multiple handlers to a single hook. In the example, two handlers are registered to 'myHook'. When 'myHook' is called, both handlers are executed in the order they were registered.

const { Hookable } = require('hookable');
const hooks = new Hookable();

hooks.hook('myHook', async () => {
  console.log('First handler executed!');
});

hooks.hook('myHook', async () => {
  console.log('Second handler executed!');
});

hooks.callHook('myHook');

Passing Arguments to Hooks

This feature allows you to pass arguments to hooks. In the example, a message is passed to 'myHook' when it is called, and the registered handler logs the message to the console.

const { Hookable } = require('hookable');
const hooks = new Hookable();

hooks.hook('myHook', async (message) => {
  console.log(message);
});

hooks.callHook('myHook', 'Hello, world!');

Handling Errors in Hooks

This feature allows you to handle errors that occur in hooks. In the example, an error is thrown in the registered handler for 'myHook'. The error is caught and logged to the console when 'myHook' is called.

const { Hookable } = require('hookable');
const hooks = new Hookable();

hooks.hook('myHook', async () => {
  throw new Error('Something went wrong!');
});

hooks.callHook('myHook').catch(err => {
  console.error(err.message);
});

Other packages similar to hookable

Keywords

FAQs

Package last updated on 30 Mar 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc